package com.pickup.controller;


import com.pickup.enums.OperatorFriendRequestTypeEnum;
import com.pickup.pojo.ChatGroup;
import com.pickup.req.vo.OperationApply;
import com.pickup.resp.JSONResult;
import com.pickup.service.ChatGroupService;
import com.pickup.service.UserService;
import com.pickup.utils.FastDFSClient;
import com.pickup.utils.UserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("g")
@Slf4j
public class GroupController {

    @Autowired
    private UserService userService;

    @Autowired
    private FastDFSClient fastDFSClient;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private UserUtil userUtil;

    @Autowired
    private ChatGroupService chatGroupService;

    // 创建群聊组
    @PostMapping("/createGroup")
    public JSONResult createGroup(@RequestBody ChatGroup chatGroup) throws Exception {
        if (chatGroup.getUserID() == null){
            return JSONResult.build(500, "创建失败！", null);
        }
        return chatGroupService.createGroup(chatGroup);
    }

    // 获取群聊所有用户信息
    @PostMapping("/findGroup")
    public JSONResult findGroup(String groupId) throws Exception {
        if (groupId == null){
            return JSONResult.build(500, "获取失败！", null);
        }
        return chatGroupService.findGroup(groupId);
    }

    // 获取所有群聊组
    @PostMapping("/findAllGroup")
    public JSONResult findAllGroup(String userId) throws Exception {
        if (userId == null){
            return JSONResult.build(500, "查找失败！", null);
        }
        return chatGroupService.findAllGroup(userId);
    }

    // 申请加入群组
    @PostMapping("/joinByGroup")
    public JSONResult joinByGroup(String groupId, String userId) throws Exception {
        if (groupId == null || userId == null){
            return JSONResult.build(500, "加入失败！", null);
        }
        return chatGroupService.joinByGroup(groupId, userId);
    }

    // 获取所有申请入群请求
    @PostMapping("/getApplyGroup")
    public JSONResult getApplyGroup(String userId) throws Exception {
        if (userId == null){
            return JSONResult.build(500, "获取失败！", null);
        }
        return chatGroupService.getApplyGroup(userId);
    }

    // 操作入群请求
    @PostMapping("/operationApplyGroup")
    public JSONResult operationApplyGroup(@RequestBody OperationApply operationApply) throws Exception {
        if (operationApply == null){
            return JSONResult.build(500, "加入失败！", null);
        }
        if (operationApply.getOperation().equals(OperatorFriendRequestTypeEnum.IGNORE.type)){
            // 判断如果忽略入群求，则直接删除请求记录
            return chatGroupService.deleteGroupRequest(operationApply);
        }
        if (operationApply.getOperation().equals(OperatorFriendRequestTypeEnum.PASS.type)){
            // 判断如果通过入群求，则提交数据
            return chatGroupService.passGroupRequest(operationApply);
        }
        return JSONResult.build(500, "操作失败！", null);
    }

    // 获取所有群聊组
    @PostMapping("/findByAllGroup")
    public JSONResult findByAllGroup(String userId) throws Exception {
        if (userId == null){
            return JSONResult.build(500, "用户为空！", null);
        }
        return chatGroupService.findByAllGroup(userId);
    }

    // 获取指定群组
    @PostMapping("/searchGroup")
    public JSONResult searchGroup(String keyword) throws Exception {
        if (keyword == null){
            return JSONResult.build(500, "没有该群组！", null);
        }
        return chatGroupService.searchGroup(keyword);
    }

}
